package com.yuanli.cosmeticmirror.persenter;

import androidx.lifecycle.LifecycleOwner;

import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.comenjoysoft.baselibrary.presenter.BasePresenter;
import com.yuanli.cosmeticmirror.model.biz.IRegisterBiz;
import com.yuanli.cosmeticmirror.model.impl.RegisterImpl;
import com.yuanli.cosmeticmirror.model.iview.IRegisterView;

public class RegisterPersenter extends BasePresenter<IRegisterView> {

    private final IRegisterBiz biz;

    public RegisterPersenter(IRegisterView view, LifecycleOwner owner) {
        attachView(view);
        biz = new RegisterImpl(owner);
    }

    public void getCode(String phone) {
        biz.getCodeModel(phone, new CallbackListener<String>() {
            @Override
            public void success(String object) {
                if (object.contains("一天最多")) {
                    mView.Error(3, "获取验证码次数超出");
                    return;
                }
                mView.getCodeSuncess(object);
            }

            @Override
            public void failed(Object object) {
                mView.Error(1, "获取验证码失败");
            }
        });
    }

    public void register(String userName, String phone, String password) {
        biz.register(userName, phone, password, new CallbackListener<String>() {
            @Override
            public void success(String object) {
                if (object.contains("SUCCESS")) {
                    mView.Sucess();
                } else if (object.contains("Account Exist")) {
                    mView.Error(5, "用户已存在");
                } else {
                    mView.Error(4, "注册失败");

                }
            }

            @Override
            public void failed(Object object) {
                mView.Error(0, "注册失败");
            }
        });
    }
}
